﻿ namespace D.Infrastructure.SystemRepositories
{
    public class SysOrgRepository : ISysOrgRepository
    { 
        /// <summary>
        /// SqlSugar 对象
        /// </summary>
        private readonly ISqlSugarClient _sqlSugarClient;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sqlSugarClient"></param>
        public SysOrgRepository(ISqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sysOrgEntity"></param>
        /// <returns></returns>
        public async Task AddAsync(SysOrgEntity sysOrgEntity)
        {
            await _sqlSugarClient.Storageable(sysOrgEntity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="guids"></param>
        /// <returns></returns>
        public async Task DeleteAsync(List<Guid> guids)
        {
            await _sqlSugarClient.Deleteable(guids).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sysOrgEntity"></param>
        /// <returns></returns>
        public async Task UpdateAsync(SysOrgEntity sysOrgEntity)
        {
            await _sqlSugarClient.Updateable(sysOrgEntity).ExecuteCommandAsync();
        }


        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageQuery"></param>
        /// <returns></returns>
        //public async Task<PageModel<SysOrgEntity>> QueryPageAsync(PageQuery pageQuery)
        //{
        //    int totalCount = 0;
        //    var res = await _sqlSugarClient.Queryable<SysOrgEntity>().ToPageListAsync(pageQuery.Current, pageQuery.PageSize, totalCount);
        //    return new PageModel<SysOrgEntity>()
        //    {
        //        Data = res,
        //        PageNo = pageQuery.Current,
        //        PageSize = pageQuery.PageSize,
        //        TotalCount = totalCount,
        //        TotalPage = (totalCount + pageQuery.PageSize - 1) / pageQuery.PageSize,
        //    };
        //}

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<SysOrgEntity>> QueryListAsync()
        {
            return await _sqlSugarClient.Queryable<SysOrgEntity>().ToListAsync();
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public async Task<SysOrgEntity> GetByIdAsync(Guid guid)
        {
            return await _sqlSugarClient.Queryable<SysOrgEntity>().Where(m => m.Id == guid).FirstAsync();
        }
    }
}
